<template>
  <div>
    <!-- 功能按钮 -->
    <el-row>
      <auth-btn btnPerm="parking:area:add_edit">
        <el-button type="primary" @click="openDialog()">添加区域</el-button>
      </auth-btn>
    </el-row>
    <!-- 表单区域 -->
    <el-table
      :data="areaDataList"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        label="序号"
        width="120"
      />
      <el-table-column
        prop="areaName"
        label="区域名称"
        width="120"
      />
      <el-table-column
        prop="spaceNumber"
        label="车位数(个)"
        show-overflow-tooltip
      />
      <el-table-column
        prop="areaProportion"
        label="面积（m²）"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ruleName"
        label="计费规则"
        show-overflow-tooltip
      />
      <el-table-column
        prop="remark"
        label="备注"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-button
            type="text"
            @click="openDialog(scope.row)"
            v-auth-dir="`parking:area:add_edit`"
          >编辑</el-button>
          <el-button
            type="text"
            @click="delAreaFn(scope.row.id)"
            v-auth-dir="`parking:area:remove`"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗区域 -->
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="580px"
      @close="closeReset"
    >
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="区域名称" prop="areaName">
            <el-input v-model="addForm.areaName" />
          </el-form-item>
          <el-form-item label="车位数（个）" prop="spaceNumber">
            <el-input v-model="addForm.spaceNumber" />
          </el-form-item>
          <el-form-item label="面积（m²）" prop="areaProportion">
            <el-input v-model="addForm.areaProportion" />
          </el-form-item>
          <el-form-item width="100%" label="关联计费规则" prop="ruleId">
            <template>
              <el-select v-model="addForm.ruleId" clearable placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              v-model="addForm.remark"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addArea">确 定</el-button>
      </template>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      class="pagination"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="params.pageSize"
      @current-change="pageChangeFn"
    />
  </div>
</template>

<script>
import { getCarRuleAPI } from '@/apis/rule'
import { addAreaAPI, getAreaListAPI, editAreaAPI, delAreaAPI } from '@/apis/area'
export default {
  data() {
    return {
      title: '添加区域',
      dialogVisible: false,
      areaDataList: [], // 数据列表
      addForm: {
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        ruleId: '',
        remark: ''
      }, // 绑定的数据
      addFormRules: {
        areaName: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        spaceNumber: [
          { required: true, message: '请输入车数个数', trigger: 'blur' }
        ],
        areaProportion: [
          { required: true, message: '请输入区域面积', trigger: 'blur' }
        ],
        ruleId: [
          { required: true, message: '请选择计费规则', trigger: 'change' }
        ]
      },
      options: [
        {
          value: '',
          label: ''
        }
      ], // 下拉选项
      params: {
        page: 1,
        pageSize: 3
      },
      total: 0 // 分页总数
    }
  },
  created() {
    this.getAreaList()
  },
  methods: {
    // 打开对话框
    async openDialog(row) {
      this.dialogVisible = true
      const res = await getCarRuleAPI()
      this.options = res.data.rows.map(item => ({
        label: item.ruleName,
        value: item.id
      }))
      if (row) {
        this.title = '编辑区域'
        const { id, areaName, spaceNumber, areaProportion, ruleId, remark } = row
        this.addForm = { id, areaName, spaceNumber, areaProportion, ruleId, remark }
      } else {
        this.title = '添加区域'
      }
    },
    // 添加区域
    async addArea() {
      await this.$refs.addForm.validate()
      let res = ''
      if (this.title === '添加区域') {
        res = await addAreaAPI(this.addForm)
        // console.log(res)
      } else {
        res = await editAreaAPI(this.addForm)
        // console.log(res)
      }
      if (res.code === 10000) {
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.getAreaList()
        this.dialogVisible = false
      }
    },
    // 获取区域管理列表
    async getAreaList() {
      const res = await getAreaListAPI(this.params)
      // console.log(res)
      this.areaDataList = res.data.rows
      this.total = res.data.total
    },
    // 分页功能
    pageChangeFn(changePage) {
      console.log(changePage)
      this.params.page = changePage
      this.getAreaList()
    },
    // 删除区域
    async delAreaFn(id) {
      const res = await delAreaAPI(id)
      console.log(res)
      this.getAreaList()
    },
    // 关闭弹窗时清空校验规则
    closeReset() {
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="postcss" scoped>
.pagination{
  margin-top: 20px;
  text-align: right;
}
</style>
